package structure;

import java.util.Arrays;

public class MyStack2<E> {
    private static final int DEFAULT_CAPACITY = 10;
    protected Object[] elem;
    protected int size;

    public MyStack2() {
        this.elem = new Object[DEFAULT_CAPACITY];
    }

    // 1.压栈
    public void push(E val) {
        if (size == elem.length) {
            elem = Arrays.copyOf(elem, elem.length * 2);
        }
        elem[size++] = val;
    }

    // 2.出栈
    public E pop() {
        if (size == 0) {
            return null;
        }
        return (E) elem[--size];
    }

    // 3.peek
    public E peek() {
        if (size == 0) {
            return null;
        }
        return (E) elem[size-1];
    }
}
